package models

import (
	"gorm.io/gorm"
	"time"
	"user_srv/basic/config"
)

type Order struct {
	Id          uint           `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;not null;" json:"id"`
	UserId      uint           `gorm:"column:user_id;type:bigint(20);comment:用户ID;not null;" json:"user_id"`                                                                 // 用户ID
	DriverId    uint           `gorm:"column:driver_id;type:bigint(20);comment:司机ID;not null;" json:"driver_id"`                                                             // 司机ID
	OrderSn     string         `gorm:"column:order_sn;type:varchar(255);comment:订单编号;not null;" json:"order_sn"`                                                           // 订单编号
	Start       string         `gorm:"column:start;type:varchar(255);comment:起始地;not null;" json:"start"`                                                                   // 起始地
	End         string         `gorm:"column:end;type:varchar(255);comment:目的地;not null;" json:"end"`                                                                       // 目的地
	TakingOrder uint           `gorm:"column:taking_order;type:tinyint(1) UNSIGNED;comment:接单模式：1:实时单，2:预约单;not null;default:1;" json:"taking_order"`                // 接单模式：1:实时单，2:预约单
	Kilometer   float64        `gorm:"column:kilometer;type:decimal(10, 2);comment:公里数;not null;" json:"kilometer"`                                                         // 公里数
	Total       float64        `gorm:"column:total;type:decimal(10, 2);comment:价格;not null;" json:"total"`                                                                   // 价格
	Status      uint           `gorm:"column:status;type:tinyint(1) UNSIGNED;comment:状态：1:待司机接单，2:已接单，3:进行中，4:已结束，5:已完成;not null;default:1;" json:"status"` // 状态：1:待司机接单，2:已接单，3:进行中，4:已结束，5:已完成
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (o *Order) TableName() string {
	return "order"
}
func GetOrder(kilometer float64, driverId int) float64 {
	var total float64
	switch driverId {
	case 1:
		total += 1.0
	case 2:
		total += 1.5
	case 3:
		total += 2.0
	default:
		total += 0.8
	}
	return kilometer * total
}
func (o *Order) CreateOrder() error {
	return config.DB.Create(&o).Error
}
func (o *Order) UpdateOrder() error {
	return config.DB.Model(&Order{}).Where("id = ?", o.Id).Update("taking_order", o.TakingOrder).Error
}
func (o *Order) GetOrderDirver() error {
	return config.DB.Where("driver_id = ?", o.DriverId).Limit(1).Find(&o).Error
}
func (o *Order) OrderDetail() error { //展示订单详情
	return config.DB.Where("id = ?", o.Id).Limit(1).Find(&o).Error
}
func (o *Order) OrderUserByMobile() error { //查询用户
	return config.DB.Where("user_id = ?", o.UserId).Limit(1).Find(&o).Error
}
func (o *Order) UpdateOrderStatus() error { //用户确认信息修改状态
	return config.DB.Model(&Order{}).Where("id = ?", o.Id).Update("status", o.Status).Error
}
func (o *Order) GetOrderId() error {
	return config.DB.Where("id = ?", o.Id).Limit(1).Find(&o).Error
}

func (o *Order) MyOrder() (MyOrderLists []*Order, err error) {
	err = config.DB.Where("user_id = ?", o.UserId).Find(&MyOrderLists).Error
	return
}
